package dao;
/*
从数据库中获取学生信息,并将学生信息封装进student类中，返回一个存student的ArrayList
 */

import domain.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import utils.JDBCUtils;

import javax.sql.DataSource;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StudentSearch {
    //获取学生信息
    public ArrayList StudentSearch() throws SQLException {
        //1.获取连接池Datasource对像
        DataSource dataSource = JDBCUtils.getDataSource();

        //2.使用DBUtils
        QueryRunner qr = new QueryRunner(dataSource);
        System.out.println("connect database and do the StudentSearch()...");
        //3.查询操作
        List<Map<String,Object>> stds_info = (List<Map<String,Object>>) qr.query("select * from student_info",new MapListHandler());
        //循环取出数值并将其封装至student类中，并存入array中
        ArrayList student_list = new ArrayList();
        for(Map<String,Object> map: stds_info){
            String sname = (String) map.get("sname");
            int sid = (int) map.get("sid");
            String sgender = (String) map.get("sgender");
            Date sbirth = (Date) map.get("sbirth");
            Student std = new Student(sname,sgender,sbirth, sid);
            student_list.add(std);
        }
        return student_list;
    }
}
